package fm.dongman.animefm.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.List;

import fm.dongman.animefm.R;
import fm.dongman.animefm.contractlib.IModel;
import fm.dongman.animefm.view.activity.SingleActivity;
import fm.dongman.animefm.view.transform.GlideRoundTransform;
import fm.dongman.animefm.view.util.ActivityHelper;
import fm.dongman.modellib.model.ComicModel;

import static fm.dongman.animefm.view.activity.SingleActivity.Companion;

/**
 * 番剧推荐
 * Created by alex on 2017/10/16.
 */

public class ComicRankInfoAdapter extends RecyclerView.Adapter<ComicRankInfoAdapter.ViewHolder> {
    private final static int STEP_COUNT = 9; // 增长值
    private Context mContext;
    private List<IModel.IComicModel> mComicModelList;
    private int mLoadCount = STEP_COUNT;
    private boolean isPageLoad = false; // 是否分页加载

    public ComicRankInfoAdapter(Context context, List<IModel.IComicModel> comicModelList) {
        mContext = context;
        mComicModelList = comicModelList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(View.inflate(mContext, R.layout.item_comic_rank_info, null));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        // 是否分页加载
        if (isPageLoad && position > mLoadCount) {
            return;
        }
        final ComicModel comic = (ComicModel) mComicModelList.get(position);
        Glide.with(mContext)
                .load(comic.getImgUrl()).bitmapTransform(new CenterCrop(mContext), new GlideRoundTransform(mContext, 2))
                .into(holder.mIvImg);
        holder.mTvTitle.setText(comic.getTitle());
        holder.mRateScore.setText(comic.getRateScore());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mContext instanceof Activity) {
                    Intent intent = new Intent(mContext, SingleActivity.class);
                    intent.putExtra(Companion.getEXTRA_NAME(), Companion.getINTENT_EXTRA_ANIME_INFO());
                    intent.putExtra(Companion.getBUNDLE_EXTRA_KEY_ID(), comic.getId());
                    ActivityHelper.INSTANCE.startActivity((Activity) mContext, intent);
                } else {
                    throw new RuntimeException(mContext.toString() + "必须是Activity!");
                }
            }
        });


    }

    @Override
    public int getItemCount() {
        if (!isPageLoad) {
            return mComicModelList.size();
        }
        //Log.i("-0-mComicModelList",mComicModelList.size()+"");
        if (mComicModelList.size() < mLoadCount) {
            mLoadCount = mComicModelList.size();
        }
        return mLoadCount;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView mTvTitle;
        ImageView mIvImg;
        TextView mRateScore; //评分

        public ViewHolder(View itemView) {
            super(itemView);
            mTvTitle = (TextView) itemView.findViewById(R.id.myTvComicTitle);
            mIvImg = (ImageView) itemView.findViewById(R.id.mIvComicPic);
            mRateScore = (TextView) itemView.findViewById(R.id.TvRateScore);
        }
    }

    /**
     * 加载更多
     *
     * @return 是否有更多数据
     */
    public boolean loadMore() {
        mLoadCount += STEP_COUNT;
        boolean canLoad = true;
        if (mLoadCount > mComicModelList.size()) {
            canLoad = false;
            mLoadCount = mComicModelList.size();
        }
        notifyDataSetChanged();
        return canLoad;
    }

    /**
     * @param isPageLoad 是否分页加载
     */
    public void isPageLoad(boolean isPageLoad) {
        this.isPageLoad = isPageLoad;
    }
}
